package services;


import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import edu.ipfw.services.patron.*;
import edu.ipfw.fitness.*;
import javax.jws.WebService;

import org.jboss.ws.annotation.EndpointConfig;

import data.DbPatron;

@WebService(serviceName = "SecurePatron", endpointInterface = "edu.ipfw.services.patron.SecurePatronService", targetNamespace = "http://ipfw.edu/services/patron")
@EndpointConfig(configName="Standard WSSecurity Endpoint")
public class SecurePatronServiceImpl implements SecurePatronService {

	DbPatron db = new DbPatron();

	public boolean checkIn(java.lang.String id) throws PatronNotFoundFault {
		try {	
		GregorianCalendar c = new GregorianCalendar();
			c.setTime(new Date());
			XMLGregorianCalendar cal;
				cal = DatatypeFactory.newInstance()
						.newXMLGregorianCalendar(c);
			PatronType e = db.getPatron(id);
					e.setCheckedIn(true);
					e.setLastCheckInDate(cal);
					return db.update(e);
			} catch (DatatypeConfigurationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return false;
	}

	
//TODO
	public boolean remove(String id) throws PatronNotFoundFault {
		return db.remove(id);
		
		}

	public PatronType register(java.lang.String id,
			java.lang.String lastName, java.lang.String firstName,
			java.lang.String email, MembershipType membershipType,
			MembershipDurationType membershipDuration) {
		PatronType p = new PatronType();
		p.setCheckedIn(false);
		p.setEmail(email);
		p.setFirstName(firstName);
		p.setLastName(lastName);
		p.setMembershipDuration(membershipDuration);
		p.setId(id);
		p.setMembershipType(membershipType);
		p.setVisitHistory(new PatronVisitListType());
		try {
			if (membershipDuration.toString().equals("SEMESTER")) {
				p.setExpirationDate(DatatypeFactory.newInstance()
						.newXMLGregorianCalendarDate(2011,8,15,DatatypeConstants.FIELD_UNDEFINED));
			} else if (membershipDuration.toString().equals("YEAR")) {
				p.setExpirationDate(DatatypeFactory.newInstance()
						.newXMLGregorianCalendarDate(2011,8,15,DatatypeConstants.FIELD_UNDEFINED));
			} else {

				p.setExpirationDate(DatatypeFactory.newInstance()
						.newXMLGregorianCalendarDate(2011,8,15,DatatypeConstants.FIELD_UNDEFINED));
			}
		} catch (DatatypeConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.addPatron(p);
		

		return p;
	}

	

	public boolean checkOut(java.lang.String id) throws PatronNotFoundFault {
		try {
			GregorianCalendar c = new GregorianCalendar();
			c.setTime(new Date());
			PatronVisitType v = new PatronVisitType();
			XMLGregorianCalendar cal = DatatypeFactory.newInstance()
					.newXMLGregorianCalendar(c);
				PatronType e = db.getPatron(id);
					e.setCheckedIn(false);
					v.setCheckInTime(e.getLastCheckInDate());
					v.setCheckOutTime(cal);
					db.addVisit(v, id);
					return db.update(e);
				
				
		} catch (DatatypeConfigurationException e) {
			e.printStackTrace();
			return false;
		}
	}



	public PatronType renew(java.lang.String id,
			MembershipDurationType membershipDuration)
			throws PatronNotFoundFault {
		PatronType p = db.getPatron(id);
		//TODO revise exp date impl
		try {
			if (membershipDuration.toString().equals("SEMESTER")) {

				p.setExpirationDate(DatatypeFactory.newInstance()
						.newXMLGregorianCalendarDate(2011,12,18,DatatypeConstants.FIELD_UNDEFINED));
			} else if (membershipDuration.toString().equals("YEAR")) {

				p.setExpirationDate(DatatypeFactory.newInstance()
						.newXMLGregorianCalendarDate(2011,12,18,DatatypeConstants.FIELD_UNDEFINED));
			} else {
				p.setExpirationDate(DatatypeFactory.newInstance()
						.newXMLGregorianCalendarDate(2011,12,18,DatatypeConstants.FIELD_UNDEFINED));
			}
		} catch (DatatypeConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.update(p);
		return p;
	}

	
	public long addVisit(java.lang.String id,
			javax.xml.datatype.XMLGregorianCalendar startDateTime,
			javax.xml.datatype.XMLGregorianCalendar endDateTime) throws PatronNotFoundFault {
		PatronVisitType v = new PatronVisitType();
		v.setCheckInTime(startDateTime);
		v.setCheckOutTime(endDateTime);
		db.addVisit(v, id);
		
		return (endDateTime.toGregorianCalendar().getTimeInMillis() - startDateTime.toGregorianCalendar().getTimeInMillis())/1000;
	}

}